#include "Adafruit_SHT31.h"
#include <Adafruit_LPS2X.h>
#include <Arduino.h>
#include <MPU6050_tockn.h>
#include <QMC5883LCompass.h>
#include <Wire.h>

/*
 *                  _          _   _             _                     _     _
 *     ___ ___   __| | ___  __| | | |__  _   _  | |__   __ _ _ __ ___ | | __| |
 *    / __/ _ \ / _` |/ _ \/ _` | | '_ \| | | | | '_ \ / _` | '__/ _ \| |/ _` |
 *   | (_| (_) | (_| |  __/ (_| | | |_) | |_| | | | | | (_| | | | (_) | | (_| |
 *    \___\___/ \__,_|\___|\__,_| |_.__/ \__, | |_| |_|\__,_|_|  \___/|_|\__,_|
 *                                       |___/

config platformio.ini deps:
lib_deps =
    adafruit/Adafruit LPS2X@^2.0.6
    mprograms/QMC5883LCompass@^1.2.3
    adafruit/Adafruit SHT31 Library@^2.2.0
    tockn/MPU6050_tockn@^1.5.2
    mathertel/OneButton@^2.6.1
    fastled/FastLED@^3.6.0

*/

Adafruit_LPS22 lsp22;
Adafruit_SHT31 sht30 = Adafruit_SHT31();
QMC5883LCompass qmc5883;
MPU6050 mpu6050(Wire1);
uint8_t lightPin = 7;

Adafruit_Sensor *lpsSensor;
int x, y, z;
sensors_event_t dataP;
void setup(void)
{
    Serial.begin(115200);

    lsp22.begin_I2C();
    sht30.begin(0x44);
    qmc5883.init();
    qmc5883.setSmoothing(10, true);

    Wire1.begin(36, 37);
    mpu6050.begin();
    mpu6050.calcGyroOffsets(true);

    lpsSensor = lsp22.getPressureSensor();
    lpsSensor->printSensorDetails();
}

void loop()
{
    lpsSensor->getEvent(&dataP);

    float t = sht30.readTemperature();
    float h = sht30.readHumidity();

    qmc5883.read();
    x = qmc5883.getX();
    y = qmc5883.getY();
    z = qmc5883.getZ();

    int sensorValue = analogRead(lightPin);
    float voltage = sensorValue * (3.3 / 4095);
    float lux = voltage / 3.3 * 1000;

    mpu6050.update();
    // ------------------------------------
    Serial.print("气压: ");
    Serial.print(dataP.pressure);
    Serial.print(" hPa");
    Serial.print("     温度: ");
    Serial.print(t);
    Serial.print("°C   湿度: ");
    Serial.print(h);
    Serial.print("%    光照: ");
    Serial.print(lux);
    Serial.print("lx     陀螺仪XYZ: ");
    Serial.print(mpu6050.getAngleX());
    Serial.print(" ");
    Serial.print(mpu6050.getAngleY());
    Serial.print(" ");
    Serial.print(mpu6050.getAngleZ());
    Serial.print("°C    地磁XYZ: ");
    Serial.print(x);
    Serial.print(" ");
    Serial.print(y);
    Serial.print(" ");
    Serial.print(z);
    Serial.println("G");

    delay(500);
}
